#!/bin/bash

# ROS2 多节点 Launch 测试脚本

echo "=== ROS2 多节点 Launch 测试程序 ==="
echo ""
echo "本脚本提供以下测试功能："
echo "- 传统多节点启动测试"
echo "- 组件间通信测试"
echo "- 性能对比测试"
echo "- 参数配置查看"
echo ""

# 设置工作空间环境
source install/setup.bash

echo "选择要启动的示例："
echo "1. 基础多节点启动 (basic_multi_launch.py)"
echo "2. 高级多节点启动 (advanced_multi_launch.py)"
echo "3. 条件启动 (conditional_launch.py)"
echo "4. 重映射启动 (remap_launch.py)"
echo "5. 综合启动 (comprehensive_launch.py)"
echo "6. 组件通信示例 (component_communication.py)"
echo "7. 组件性能测试 (component_performance_test.py)"
echo "8. 组件 vs 传统节点对比 (component_launch.py)"
echo "9. 查看所有启动参数"
echo ""

read -p "请输入选择 (1-9): " choice

case $choice in
    1)
        echo "启动基础多节点示例..."
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo basic_multi_launch.py"
        ;;
    2)
        echo "启动高级多节点示例..."
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo advanced_multi_launch.py"
        ;;
    3)
        echo "启动条件启动示例..."
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo conditional_launch.py start_service:=true"
        ;;
    4)
        echo "启动重映射示例..."
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo remap_launch.py topic_name:=custom_topic"
        ;;
    5)
        echo "启动综合示例..."
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo comprehensive_launch.py start_delayed:=true"
        ;;
    6)
        echo "启动组件通信示例..."
        echo "展示组件间高效通信流程：发布者 -> 处理器 -> 聚合器 -> 消费者"
        echo "监控主题：/raw_data, /processed_data, /aggregated_data, /monitor_report"
        echo "按 Ctrl+C 停止测试"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_communication.py"
        ;;
    7)
        echo "启动组件性能测试..."
        echo "选择测试模式："
        echo "  a) 高频通信测试 (high_frequency) - 测试高频率消息传输"
        echo "  b) 压力测试 (stress_test) - 测试多发布者并发处理"
        echo "  c) 延迟测试 (latency_test) - 测试通信延迟"
        read -p "请选择测试模式 (a/b/c): " test_mode
        case $test_mode in
            a)
                echo "运行高频通信测试..."
                bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_performance_test.py test_mode:=high_frequency message_rate:=100.0"
                ;;
            b)
                echo "运行压力测试..."
                bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_performance_test.py test_mode:=stress_test"
                ;;
            c)
                echo "运行延迟测试..."
                bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_performance_test.py test_mode:=latency_test"
                ;;
            *)
                echo "无效选择，运行默认高频测试..."
                bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_performance_test.py test_mode:=high_frequency"
                ;;
        esac
        ;;
    8)
        echo "启动组件 vs 传统节点对比测试..."
        echo "选择运行模式："
        echo "  a) 组件模式 (use_component=true) - 单进程多组件，高性能"
        echo "  b) 传统节点模式 (use_component=false) - 多进程，高隔离"
        read -p "请选择模式 (a/b): " mode_choice
        case $mode_choice in
            a)
                echo "运行组件模式..."
                bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_launch.py use_component:=true"
                ;;
            b)
                echo "运行传统节点模式..."
                bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_launch.py use_component:=false"
                ;;
            *)
                echo "无效选择，运行组件模式..."
                bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_launch.py use_component:=true"
                ;;
        esac
        ;;
    9)
        echo "=== 所有启动文件的参数 ==="
        echo ""
        echo "1. basic_multi_launch.py:"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo basic_multi_launch.py --show-args"
        echo ""
        echo "2. advanced_multi_launch.py:"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo advanced_multi_launch.py --show-args"
        echo ""
        echo "3. conditional_launch.py:"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo conditional_launch.py --show-args"
        echo ""
        echo "4. remap_launch.py:"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo remap_launch.py --show-args"
        echo ""
        echo "5. comprehensive_launch.py:"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo comprehensive_launch.py --show-args"
        echo ""
        echo "6. component_communication.py:"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_communication.py --show-args"
        echo ""
        echo "7. component_performance_test.py:"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_performance_test.py --show-args"
        echo ""
        echo "8. component_launch.py:"
        bash -c "source install/setup.bash && ros2 launch multi_launch_demo component_launch.py --show-args"
        ;;
    *)
        echo "无效选择，请重新运行脚本"
        echo ""
        echo "使用方法："
        echo "  ./run_test_multi_launch_demo.sh"
        echo ""
        echo "测试完成后，可以使用以下命令查看系统状态："
        echo "  ros2 node list          # 查看运行中的节点"
        echo "  ros2 topic list         # 查看所有主题"
        echo "  ros2 topic echo /topic  # 监听特定主题"
        echo "  ros2 service list       # 查看所有服务"
        echo "  ros2 param list         # 查看所有参数"
        ;;
esac

echo ""
echo "=== 测试完成 ==="
echo "如需查看系统状态，请使用以下命令："
echo "  ros2 node list"
echo "  ros2 topic list"
echo "  ros2 topic echo /raw_data"
echo "  ros2 topic echo /processed_data"
echo "  ros2 topic echo /aggregated_data"
echo "  ros2 topic echo /monitor_report"
